package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.utils.APPUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by admin on 2016/1/14.
 */
public class RechargeAdapter extends RecyclerView.Adapter<RechargeAdapter.MyViewHolder>
{

    private Activity activity;
    private JSONArray list;
    private OnItemClickListener listener;
    private int SelectPosition;
    public RechargeAdapter(Activity activity, JSONArray list){
        this.activity = activity;
        this.list = list;
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                activity).inflate(R.layout.activity_recharge_item, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position)
    {
        JSONObject price = list.optJSONObject(position);
        holder.txtPersonCoin.setText(price.optString("PersonCoin")+"元");
        holder.txtGameCoin.setText("(" + price.optString("GameCoin") + ")");
        holder.txtPersonCoin.setTextColor(APPUtils.getResourcesColor(activity, R.color.black));
        holder.txtGameCoin.setTextColor(APPUtils.getResourcesColor(activity, R.color.grey2));
        holder.vContent.setBackgroundResource(R.drawable.shape_border_gray2);
        if(price.optInt("select",-1)!=-1){
            holder.vContent.setBackgroundResource(R.color.orange);
            holder.txtPersonCoin.setTextColor(APPUtils.getResourcesColor(activity, R.color.wihte));
            holder.txtGameCoin.setTextColor(APPUtils.getResourcesColor(activity,R.color.wihte));
        }
        holder.vContent.setTag(price);
        RxView.clicks(holder.vContent).map(View->holder.vContent).subscribe(v->{
            try {
                Log.i("dd", "--ddonItemClick:" + position+":"+SelectPosition);
                list.optJSONObject(SelectPosition).put("select",-1);
                SelectPosition = position;
                price.put("select",1);
                if(listener!=null){
                    listener.onItemClick((JSONObject) holder.vContent.getTag());
                }
                notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        });
    }

    @Override
    public int getItemCount()
    {
        return list==null?0:list.length();
    }

    class MyViewHolder extends RecyclerView.ViewHolder
    {
        View vContent;
        TextView txtPersonCoin,txtGameCoin;

        public MyViewHolder(View view)
        {
            super(view);
            vContent = view.findViewById(R.id.vContent);
            txtPersonCoin = (TextView) view.findViewById(R.id.txtPersonCoin);
            txtGameCoin = (TextView) view.findViewById(R.id.txtGameCoin);

        }
    }

    public interface OnItemClickListener{
        void onItemClick(JSONObject value);
    }
}
